*****************************************************************
*****************************************************************
*** 0. General settings
*****************************************************************
*****************************************************************

* clear
clear
clear 		mata
mat 		drop _all

* set path
global		path	""					

	
	use "$path/0 Data/_Processed/InputUsageAnalysis/drawdown_qrt.dta", clear

		generate 	quarter = 	.	
		replace 	quarter = 	1 if month == 3
		replace 	quarter = 	2 if month == 6
		replace 	quarter = 	3 if month == 9
		replace 	quarter = 	4 if month == 12
			
		generate	yq 		= 	yq(year, quarter)
		format 		yq 			%tq	
			
			
		collapse 	(mean) UsagePctBoth, by(yq)
		drop 		if UsagePctBoth == .
			
		merge 		1:1 yq using "$path/0 Data/_Processed/sp500-ret-q"	
		keep 		if _merge == 3
		drop 		_merge

		merge 		1:1 yq using "$path/0 Data/_Processed/sp500-ret-q-lowest-cum"	
		keep 		if _merge == 3
		drop 		_merge
		
		
		tsset 		yq	
	
	
		* Add Q1 2020 as additional observation
		
		
		set 		obs 			`=	_N+1'
		replace 	yq 				= 	240 		if 	yq	==	.
		
		replace 	UsagePctBoth	=	0.381 		if 	UsagePctBoth==. 		// from the drawdown data (quarterly)
		replace 	return_sp_q		=	-0.2231567 	if 	return_sp_q==.
		replace 	return_sp_q_low		=	-0.3674092 	if 	return_sp_q_low==.
		
		
*		twoway (scatter UsagePctBoth return_sp_q) (lfit  UsagePctBoth return_sp_q )	
	
	
		set 		obs 			`=	_N+1'
		replace 	yq 				= 	239 		if 	yq	==	.
		
		replace 	UsagePctBoth	=	0.188 		if 	UsagePctBoth==. 		// from the drawdown data (quarterly)
		replace 	return_sp_q		=	.08189487	if 	return_sp_q==.
		replace 	return_sp_q_low		=	-.03039953 	if 	return_sp_q_low==.
	
	
		generate	covid = (yq >= 239)
		
		
		
				* Using 2 regression slopes
			
			twoway (scatter UsagePctBoth return_sp_q if covid == 0, mlabel(yq) ) || (scatter UsagePctBoth return_sp_q if covid == 1, mlabel(yq) ) || (lfit  UsagePctBoth return_sp_q  if covid == 0)  || (lfit  UsagePctBoth return_sp_q if covid == 1 ) ,	///
 					scheme(s2mono) ///
					xtitle("S&P 500 quarterly return")		///
					ytitle("Quarterly drawdown (% Total)")		///
					title("Credit Line Drawdowns during 2007-2009 & COVID") ///
					ylabel(, angle(horizontal)) ///
					note("")   ///
					legend(off) ///
					graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
			graph 	export  "$path/05 Figures/Figure 6 Panel A.png",replace				
			
			
			
			
				* Using 2 regression slopes

	twoway (scatter UsagePctBoth return_sp_q_low if covid == 0, mlabel(yq) ) || (scatter UsagePctBoth return_sp_q_low if covid == 1, mlabel(yq) ) || (lfit  UsagePctBoth return_sp_q_low  if covid == 0)  || (lfit  UsagePctBoth return_sp_q_low if covid == 1 ) ,	///
 					scheme(s2mono) ///
					xtitle("(Lowest) S&P 500 quarterly return")		///
					ytitle("Quarterly drawdown (% Total)")		///
					title("Credit Line Drawdowns during 2007-2009 & COVID") ///
					ylabel(, angle(horizontal)) ///
					note("")   ///
					legend(off) ///
					graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
			graph 	export  "$path/05 Figures/Figure 6 Panel B.png",replace	
			
			